package com.myrealmfrom;

import com.qf.entity.UserEmp;
import com.qf.entity.UserRole;
import com.qf.service.UserLoginExitService;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 2019/11/26
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */

//登录成功是被调用
public class ShiroFrom extends FormAuthenticationFilter {

    //得到当前用户的角色
    //得到当前用户的菜单
    @Autowired
    private UserLoginExitService service;


    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {

        System.out.println("登录开始");
        //得到当前用户的登录信息
        UserEmp userEmp=(UserEmp) subject.getPrincipal();
        //获得用户菜单、权限 存入session
        UserRole userRole = service.getMenu(userEmp.getRoleId());

        HttpServletRequest shiroRequest= (HttpServletRequest) request;

        HttpSession session = shiroRequest.getSession();

        //将用户信息和角色信息存放在session中
        session.setAttribute("menu",userRole);
        session.setAttribute("user",userEmp);

        HttpServletResponse shiroResponse= (HttpServletResponse) response;

       /* shiroResponse.sendRedirect("/index.jsp");*/

        System.out.println("登录结束");

        return false;
    }
}
